//
//  HRFillInformationViewController.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
// 完善信息 

#import "HRFillInformationViewController.h"
#import "HRShowSelectSexView.h"
#import "HREnterTextFiledView.h"
#import "HRIconButton.h"
#import "HRRegestViewModel.h"
#import "HRVerificationCodeModel.h"
#import "HRRegisterModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HRLoginRootViewModel.h"
#import "HRLoginModel.h"
#import "HearNavigationController.h"
#import "HRCoverImageModel.h"
#import "HRTabBarViewController.h"
#import "HREnterInvitationCodeViewController.h"
#import <TPKeyboardAvoidingScrollView.h>
#import "ZHDatePickerView.h"
#import "HRRegestViewController.h"
#import "UzysImagePickerController.h"
#import "VPImageCropperViewController.h"
#import "OSSImageUploader.h"
#import "MBProgressHUD+assistant.h"
#import "HRShowLabelView.h"
#import "HRUserLabelModel.h"
#import "HRPostDynamicViewModel.h"
#import "HRDynamicModel.h"
#import "HRPostSoundModel.h"
#import "HRPostSoundViewModel.h"
@interface HRFillInformationViewController ()<ZHDatePickerViewDelegate,UzysImagePickerControllerDelegate,VPImageCropperDelegate>
@property (nonatomic, strong)UIButton *iconImageView;
@property (nonatomic, strong)HREnterTextFiledView *nickNameTextFiledView;
@property (nonatomic, copy)NSString *selectIconName;
@property (nonatomic, copy)NSString *selectIconColor;
@property (nonatomic, strong)HRRegestViewModel *regesterModel;
@property (nonatomic, copy)NSString *sexStr;
@property (nonatomic, strong)HRLoginRootViewModel *viewModel;
@property(nonatomic, strong)TPKeyboardAvoidingScrollView *scrollView;
@property(nonatomic, strong)UIButton *birthdayButton;

@property(nonatomic, strong)UIImage *selectIconImage;
@property(nonatomic, strong)NSMutableArray *labelContentArr;
@property(nonatomic, strong)NSArray *selectLabelArr;
@property(nonatomic, strong)NSMutableArray *labelButtonArr;
@property(nonatomic, strong)UIButton *label1Button;
@property(nonatomic, strong)UIButton *label2Button;
@property(nonatomic, strong)UIButton *label3Button;
@property(nonatomic, strong)UIButton *enterButton;
@property(nonatomic, strong)HRPostSoundViewModel *saveSoundModel;

@end

@implementation HRFillInformationViewController

-(HRPostSoundViewModel *)saveSoundModel{
    if (_saveSoundModel == nil){
        _saveSoundModel = [HRPostSoundViewModel new];
    }
    return _saveSoundModel;
}

-(HRLoginRootViewModel *)viewModel
{
    if (_viewModel == nil)
    {
        _viewModel = [HRLoginRootViewModel new];
    }
    return _viewModel;
}

-(HRRegestViewModel *)regesterModel
{
    if (_regesterModel == nil)
    {
        _regesterModel = [HRRegestViewModel new];
    }
    return _regesterModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.labelContentArr = [NSMutableArray array];
    self.leftButton.hidden = YES;

    [self setupView];
    

}




- (void)setupView
{

    
    self.scrollView = [TPKeyboardAvoidingScrollView new];
    [self.view addSubview:self.scrollView];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"Hi，欢迎来到Hearer的世界";
    titleLabel.textColor = FirstTextColor;
    titleLabel.font = CustomUIFont(20);
    [self.scrollView addSubview:titleLabel];
//    UILabel *descLabel = [UILabel new];
//    descLabel.text = @"请选择您喜欢的头像";
//    descLabel.textColor = SecondTextColor;
//    descLabel.font = CustomUIFont(18);
//    [self.scrollView addSubview:descLabel];
    
    
    UIButton *iconImageView = [UIButton new];
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    iconImageView.clipsToBounds = YES;
    [self.scrollView addSubview:(self.iconImageView = iconImageView)];
    iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    iconImageView.layer.borderWidth = 1;
    iconImageView.layer.cornerRadius = 130 * scaleHeight / 2;
    [iconImageView addTarget:self action:@selector(selectIconClock) forControlEvents:UIControlEventTouchUpInside];
    [iconImageView setImage:[UIImage imageNamed:@"点击-选择头像"] forState:0];
    
    
    UILabel *nickNameLabel = [UILabel new];
    nickNameLabel.text = @"个性签名";
    nickNameLabel.textColor = FirstTextColor;
    nickNameLabel.font = CustomUIFont(18);
    [self.scrollView addSubview:nickNameLabel];
    
    self.nickNameTextFiledView = [HREnterTextFiledView new];
    self.nickNameTextFiledView.leftImageView.size = CGSizeZero;
    [self.scrollView addSubview:self.nickNameTextFiledView];
    self.nickNameTextFiledView.textField.placeholder = @"请输入2-20字符长度的个性签名";
    [self.nickNameTextFiledView.textField setValue:FirstTextColor forKeyPath:@"_placeholderLabel.textColor"];
    self.nickNameTextFiledView.textField.textColor = FirstTextColor;
    self.nickNameTextFiledView.line.backgroundColor = FirstTextColor;
    self.nickNameTextFiledView.textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    
    UILabel *birthdayDescLabel = [UILabel new];
    birthdayDescLabel.text = @"您的年龄";
    birthdayDescLabel.textColor = FirstTextColor;
    birthdayDescLabel.font = CustomUIFont(18);
    [self.scrollView addSubview:birthdayDescLabel];
    
    
    UIButton *birthdayButton = [UIButton buttonWithType:0];
    [birthdayButton setTitle:@"输入您的出生日期" forState:0];
    [birthdayButton setTitleColor:SecondTextColor forState:0];
    birthdayButton.titleLabel.font = CustomUIFont(15);
    [birthdayButton addTarget:self action:@selector(birthdayButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:(self.birthdayButton=birthdayButton)];
    birthdayButton.layer.borderColor = SecondTextColor.CGColor;
    birthdayButton.layer.borderWidth = 1;
    birthdayButton.layer.cornerRadius = 3;
    
    
    UILabel *signLabel = [UILabel new];
    signLabel.text = @"我的标签";
    signLabel.textColor = FirstTextColor;
    signLabel.font = CustomUIFont(18 * scaleWidth);
    [self.scrollView addSubview:signLabel];
    
    
    UIButton *signButton = [UIButton buttonWithType:0];
    [signButton setTitle:@"+ 我的个性标签" forState:0];
    [signButton setTitleColor:SecondTextColor forState:0];
    signButton.titleLabel.font = CustomUIFont(15);
    [signButton addTarget:self action:@selector(signButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [signButton sizeToFit];
    [self.scrollView addSubview:signButton];

    self.label1Button = [UIButton buttonWithType:0];
    [self.label1Button setTitleColor:FirstTextColor forState:0];
    self.label1Button.titleLabel.font = CustomUIFont(13);
    [self.scrollView addSubview:self.label1Button];
    self.label1Button.layer.cornerRadius = 3;
    self.label1Button.layer.borderWidth = 1;
    self.label1Button.layer.borderColor = FirstTextColor.CGColor;
    
    self.label2Button = [UIButton buttonWithType:0];
    [self.label2Button setTitleColor:FirstTextColor forState:0];
    self.label2Button.titleLabel.font = CustomUIFont(13);
    [self.scrollView addSubview:self.label2Button];
    self.label2Button.layer.cornerRadius = 3;
    self.label2Button.layer.borderWidth = 1;
    self.label2Button.layer.borderColor = FirstTextColor.CGColor;
    
    self.label3Button = [UIButton buttonWithType:0];
    [self.label3Button setTitleColor:FirstTextColor forState:0];
    self.label3Button.titleLabel.font = CustomUIFont(13);
    [self.scrollView addSubview:self.label3Button];
    self.label3Button.layer.cornerRadius = 3;
    self.label3Button.layer.borderWidth = 1;
    self.label3Button.layer.borderColor = FirstTextColor.CGColor;
    
    self.label1Button.hidden = YES;
    self.label2Button.hidden = YES;
    self.label3Button.hidden = YES;

    
    UIButton *enterButton = [UIButton buttonWithType:1];
    [enterButton setTitle:@"提交" forState:UIControlStateNormal];
    [enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    enterButton.titleLabel.font = CustomUIFont(15);
    [enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:(self.enterButton=enterButton)];
    enterButton.backgroundColor =MainColor;
    enterButton.layer.cornerRadius = 5;
    enterButton.clipsToBounds = YES;
    
    
    
    
    WeakSelf
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(40 * scaleHeight);
        make.left.equalTo(weakSelf.view).offset(20 * scaleWidth);
        make.width.equalTo(weakSelf.view);
        make.height.mas_equalTo(40 * scaleHeight);
    }];
    
    
//    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(titleLabel);
//        make.left.equalTo(titleLabel.mas_left);
//        make.top.equalTo(titleLabel.mas_bottom);
//        make.height.mas_equalTo(30);
//    }];
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.top.equalTo(titleLabel.mas_bottom).offset(30 * scaleHeight);
        make.size.mas_equalTo(CGSizeMake(130 * scaleHeight , 130 * scaleHeight ));
    }];
    
    [nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left);
        make.top.equalTo(iconImageView.mas_bottom).offset(30 * scaleHeight);
        make.width.height.equalTo(titleLabel);
    }];
    [self.nickNameTextFiledView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left);
        make.top.equalTo(nickNameLabel.mas_bottom).offset(15);
        make.height.mas_equalTo(41 * scaleHeight);
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
    
    
    [birthdayDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nickNameTextFiledView.mas_left);
        make.top.equalTo(weakSelf.nickNameTextFiledView.mas_bottom).offset(15);
        make.width.equalTo(titleLabel);
        make.height.mas_equalTo(30 * scaleHeight);
    }];
    
    [birthdayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(birthdayDescLabel.mas_left);
        make.top.equalTo(birthdayDescLabel.mas_bottom).offset(15);
        make.width.mas_equalTo(130 * scaleWidth);
        make.height.mas_equalTo(35 * scaleHeight);
    }];
    
    
    [signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(birthdayButton.mas_left);
        make.top.equalTo(birthdayButton.mas_bottom).offset(15);
        make.width.mas_equalTo(80 * scaleWidth);
        make.height.mas_equalTo(30 * scaleHeight);
    }];
    
    
    [signButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(signLabel.mas_right);
        make.top.equalTo(signLabel.mas_top);
        make.width.equalTo(@(130 * scaleWidth));
        make.height.mas_equalTo(30 * scaleHeight);
    }];
    
    [self.label1Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(signLabel.mas_left);
        make.top.equalTo(signButton.mas_bottom).offset(15);
        make.width.equalTo(@(75 * scaleWidth));
        make.height.mas_equalTo(30 * scaleHeight);
    }];
    
    [self.label2Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.label1Button.mas_right).offset(5 * scaleWidth);
        make.top.equalTo(weakSelf.label1Button.mas_top);
        make.width.equalTo(weakSelf.label1Button.mas_width);
        make.height.equalTo(weakSelf.label1Button.mas_height);
    }];
    [self.label3Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.label2Button.mas_right).offset(5 * scaleWidth);
        make.top.equalTo(weakSelf.label1Button.mas_top);
        make.width.equalTo(weakSelf.label1Button.mas_width);
        make.height.equalTo(weakSelf.label1Button.mas_height);
    }];
    
    [enterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.nickNameTextFiledView);
        make.top.equalTo(weakSelf.label1Button.mas_bottom).offset(30 * scaleHeight );
        make.height.mas_equalTo(40 * scaleHeight);
    }];
    

    
}

- (void)signButtonClick{
    [self.view endEditing:NO];
    if (!self.sexStr.length){
        [MBProgressHUD showMessage:@"请先选择性别" finishBlock:nil];
        return;
    }
    WeakSelf
    [HRShowLabelView showLabelView:self.labelContentArr selectIndexBlock:^(NSArray *selectArr) {
        if (selectArr.count >3){
            [MBProgressHUD showMessage:@"标签不能超过三个" finishBlock:nil];
        }
        weakSelf.selectLabelArr = selectArr;
        weakSelf.label1Button.hidden = YES;
        weakSelf.label2Button.hidden = YES;
        weakSelf.label3Button.hidden = YES;

        for (int i =0; i < weakSelf.selectLabelArr.count; i++) {
            if (i == 0){
                [weakSelf.label1Button setTitle:weakSelf.selectLabelArr[i] forState:0];
                weakSelf.label1Button.hidden = NO;
            }else if (i == 1){
                [weakSelf.label2Button setTitle:weakSelf.selectLabelArr[i] forState:0];
                weakSelf.label2Button.hidden = NO;
            }else if (i ==2){
                [weakSelf.label3Button setTitle:weakSelf.selectLabelArr[i] forState:0];
                weakSelf.label3Button.hidden = NO;
            }
        }
    }];
    
    
}


- (void)birthdayButtonClick{
    [self.view endEditing:NO];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *string = [formatter stringFromDate:date];
    NSDate *date2 = [formatter dateFromString:@"1990-09-09"];
    ZHDatePickerView *pickerView = [[ZHDatePickerView alloc] initDatePickerWithDefaultDate:date2
                                                                         andDatePickerMode:UIDatePickerModeDate];
    
    pickerView.delegate = self;
    [pickerView show];
}

- (void)pickerView:(ZHDatePickerView *)pickerView didSelectDateString:(NSString *)dateString
{
    NSString *subStr = [dateString substringToIndex:4];
    if ([subStr intValue]< 1960 || [subStr intValue] > 2001){
        [MBProgressHUD showMessage:@"年龄不在范围内" finishBlock:nil];
        return;
    }
    [self.birthdayButton setTitle:dateString forState:UIControlStateNormal];
}


- (void)selectIconClock
{
    WeakSelf
    [HRShowSelectSexView showSelectSexView:^(NSString *selectStr) {
        weakSelf.sexStr = selectStr;
        NSArray *contentArr;
        if ([selectStr isEqualToString:@"男"]){
            contentArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"manTagList"];
        }else{
            contentArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"womanTagList"];
        }
        for (NSString *str in contentArr) {
            HRUserLabelModel *model = [HRUserLabelModel new];
            model.content = str;
            [self.labelContentArr addObject:model];
        }
        
        UzysImagePickerController * UzysImageP = [[UzysImagePickerController alloc] init];
        [UzysImageP showInViewContrller:weakSelf maxCount:1 delegate:weakSelf];
    }];
}

- (void)UzysImagePickerDidFinishWithImages:(NSArray *)imageArray{
    dispatch_async(dispatch_get_main_queue(), ^{
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:[imageArray firstObject] cropFrame:CGRectMake(0, 100, kFullWidth, kFullWidth) limitScaleRatio:3.0];
        imgCropperVC.delegate = self;
        [self presentViewController:imgCropperVC animated:YES completion:nil];
    });
}


- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage{
    dispatch_async(dispatch_get_main_queue(), ^{
        [cropperViewController dismissViewControllerAnimated:YES completion:nil];
        [self.iconImageView setImage:[self zipScaleWithImage:editedImage] forState:0];
        self.selectIconImage = [self zipScaleWithImage:editedImage];
    });
}


- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController{
    dispatch_async(dispatch_get_main_queue(), ^{
        [cropperViewController dismissViewControllerAnimated:YES completion:nil];
    });
}

-(UIImage *)zipScaleWithImage:(UIImage *)sourceImage{
    NSData *data=UIImageJPEGRepresentation(sourceImage, 1.0);
    if (data.length>100*1024) {
        if (data.length>1024*1024) {//1M以及以上
            data=UIImageJPEGRepresentation(sourceImage, 0.7);
        }else if (data.length>512*1024) {//0.5M-1M
            data=UIImageJPEGRepresentation(sourceImage, 0.8);
        }else if (data.length>200*1024) {
            //0.25M-0.5M
            data=UIImageJPEGRepresentation(sourceImage, 0.9);
        }
    }
    UIImage *newImage = [UIImage imageWithData:data];
    return newImage;
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:NO];
}

- (void)enterButtonClick
{
    [MBProgressHUD showHUDAddedTo:WINDOW animated:YES];

    if (!self.selectIconImage)
    {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        [MBProgressHUD showMessage:@"请选择一个头像" finishBlock:nil];
        return;
    }
    
    if (self.nickNameTextFiledView.textField.text.length < 2 || self.nickNameTextFiledView.textField.text.length > 20)
    {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        [MBProgressHUD showMessage:@"请输入2-20字符长度的个性签名" finishBlock:nil];
        return;
    }
    
    if (self.birthdayButton.titleLabel.text.length == 8){
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        [MBProgressHUD showMessage:@"请输入您的出生日期" finishBlock:nil];
        return;
    }
    
    if (self.selectLabelArr.count >3){
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        [MBProgressHUD showMessage:@"标签不能超过三个" finishBlock:nil];
        return;
    }
    if (!self.selectLabelArr.count){
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        [MBProgressHUD showMessage:@"请选择一个标签" finishBlock:nil];
        return;
    }
    
    [OSSImageUploader asyncUploadImage:self.selectIconImage complete:^(NSArray<NSString *> *names, UploadImageState state) {
        if (state == UploadImageFailed){
            [MBProgressHUD hideHUDForView:WINDOW animated:YES];
            return;
        }
        NSString *iconUrl = [NSString stringWithFormat:@"http://%@.%@%@",BucketName,AliYunHost,[names firstObject]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.codeModel){
                [self phoneRegister:iconUrl];
            }else{
                [self otherRefister:iconUrl];
            }
        });
    }];


    
    
    
}

-(NSString *)dateToOld:(NSString  *)bornDay{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *bornDate = [dateFormatter dateFromString:bornDay];
    
    
    //获得当前系统时间
    NSDate *currentDate = [NSDate date];
    //获得当前系统时间与出生日期之间的时间间隔
    NSTimeInterval time = [currentDate timeIntervalSinceDate:bornDate];
    //时间间隔以秒作为单位,求年的话除以60*60*24*356
    int age = ((int)time)/(3600*24*365);
    return [NSString stringWithFormat:@"%d",age];
}


#pragma 第三方注册
- (void)otherRefister:(NSString *)iconUrl{
    [MBProgressHUD hideHUDForView:WINDOW animated:YES];
    HRRegisterRequstModel *paramModel = [HRRegisterRequstModel new];
    paramModel.WeiXinId = self.respDict[@"WeiXinId"];
    paramModel.QQId = self.respDict[@"QQId"];
    paramModel.WeiBoId = self.respDict[@"WeiBoId"];
    paramModel.MobileNumber = self.Mobile;
    paramModel.Password = @"";
    paramModel.LocalId = @"";
    paramModel.RegisterDateTime =[self getCurrentTime];
    paramModel.NickName = self.nickNameTextFiledView.textField.text;
    paramModel.RealName = @"听见";
    paramModel.UserPictureName = iconUrl;
    paramModel.UserPictureBackgroundColor = @"";
    paramModel.UserSoundUrl = [[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString];
    if ([self.sexStr isEqualToString:@"男"])
    {
        paramModel.Sex = @"1";
    }else{
        paramModel.Sex = @"0";
    }
    
    paramModel.Province = @"听见小镇";
    paramModel.City = @"听见小镇";
    paramModel.UserLevel = @"0";
    paramModel.UserCoverPicture = [[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideImageUrl"] absoluteString];
    
    paramModel.Birthday = self.birthdayButton.titleLabel.text;
    paramModel.Age = [self dateToOld:self.birthdayButton.titleLabel.text];
    
    paramModel.UserTags = [self.selectLabelArr jsonStringEncoded];
    WeakSelf
    [self.regesterModel registerUserParam:paramModel andResult:^(NSDictionary *result) {
        if (result)
        {
            [[NSUserDefaults standardUserDefaults]setObject:result[@"BodyContent"] forKey:@"userInfo"];
            
            HRUpdataUserInfoModel*model = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
            
            
            [[NSUserDefaults standardUserDefaults] setObject:model.LocalId forKey:@"userid"];
            [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureName forKey:@"iconName"];
            [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureBackgroundColor forKey:@"iconColor"];
            [[NSUserDefaults standardUserDefaults] setObject:model.MobileNumber forKey:@"MobileNumber"];
            [[NSUserDefaults standardUserDefaults] setObject:model.Sex forKey:@"sex"];
            [[NSUserDefaults standardUserDefaults] setObject:model.NickName forKey:@"nickName"];
            [[NSUserDefaults standardUserDefaults] setURL:[NSURL URLWithString:model.UserSoundUrl] forKey:@"SoundUrl"];


            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                HREnterInvitationCodeViewController *codeVC = [HREnterInvitationCodeViewController new];
                [weakSelf.navigationController pushViewController:codeVC animated:YES];
            });
            [JPUSHService setTags:nil alias:USERID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
                DDLog(@"iResCode==%d iAlias====%@",iResCode,iAlias);
            }];
            if ([[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString] length]){
                [weakSelf postDynamic:[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString]];
            }
            

        }else{
            [MBProgressHUD showMessage:@"注册失败，请重试" finishBlock:nil];
        }
    }];
}


#pragma 手机号注册
- (void)phoneRegister:(NSString *)iconUrl{
    HRRegisterRequstModel *paramModel = [HRRegisterRequstModel new];
    paramModel.WeiXinId = @"";
    paramModel.QQId = @"";
    paramModel.WeiBoId = @"";
    paramModel.MobileNumber = self.codeModel.Mobile;
    paramModel.Password = self.codeModel.password;
    paramModel.LocalId = @"";
    paramModel.RegisterDateTime =[self getCurrentTime];
    paramModel.NickName = self.nickNameTextFiledView.textField.text;
    paramModel.RealName = @"听见";
    paramModel.UserPictureName = iconUrl;
    paramModel.UserPictureBackgroundColor = @"";
    paramModel.UserSoundUrl = [[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString];
    if ([self.sexStr isEqualToString:@"男"])
    {
        paramModel.Sex = @"1";
    }else{
        paramModel.Sex = @"0";
    }
    
    paramModel.Province = @"听见小镇";
    paramModel.City = @"听见小镇";
    paramModel.UserLevel = @"0";
    paramModel.UserCoverPicture = @"";
    paramModel.Birthday = self.birthdayButton.titleLabel.text;
    paramModel.Age = [self dateToOld:self.birthdayButton.titleLabel.text];
    paramModel.UserTags = [self.selectLabelArr jsonStringEncoded];
    WeakSelf
    [self.regesterModel registerUserParam:paramModel andResult:^(NSDictionary *result) {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        if (result) {
            [[NSUserDefaults standardUserDefaults]setObject:result[@"BodyContent"] forKey:@"userInfo"];
            
            HRUpdataUserInfoModel*model = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
            
            
            [[NSUserDefaults standardUserDefaults] setObject:model.LocalId forKey:@"userid"];
            [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureName forKey:@"iconName"];
            [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureBackgroundColor forKey:@"iconColor"];
            [[NSUserDefaults standardUserDefaults] setObject:model.MobileNumber forKey:@"MobileNumber"];
            [[NSUserDefaults standardUserDefaults] setObject:model.Sex forKey:@"sex"];
            [[NSUserDefaults standardUserDefaults] setObject:model.NickName forKey:@"nickName"];
            [[NSUserDefaults standardUserDefaults] setURL:[NSURL URLWithString:model.UserSoundUrl] forKey:@"SoundUrl"];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                HREnterInvitationCodeViewController *codeVC = [HREnterInvitationCodeViewController new];
                [weakSelf.navigationController pushViewController:codeVC animated:YES];
            });
            [JPUSHService setTags:nil alias:USERID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
                DDLog(@"iResCode==%d iAlias====%@",iResCode,iAlias);
            }];
            if ([[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString] length]){
                [weakSelf postDynamic:[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString]];
            }
        }else{
            [MBProgressHUD showMessage:@"注册失败，请重试" finishBlock:nil];
        }
    }];
}


- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

    return [formatter stringFromDate:[NSDate date]];
}

- (void)loginSightin {
    WeakSelf
    HRLoginRequestModel *model = [HRLoginRequestModel new];
    model.MobileNumber = self.codeModel.Mobile;
    model.Password = self.codeModel.password;
    [self.viewModel phoneLoginParam:model loginSeccess:^(BOOL loginSeccess) {
        [MBProgressHUD hideHUDForView:WINDOW animated:YES];
        if (loginSeccess)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                HREnterInvitationCodeViewController *codeVC = [HREnterInvitationCodeViewController new];
                [weakSelf.navigationController pushViewController:codeVC animated:YES];
            });
            [JPUSHService setTags:nil alias:USERID fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
                DDLog(@"iResCode==%d iAlias====%@",iResCode,iAlias);
            }];
            if ([[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString] length]){
                [weakSelf postDynamic:[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideSoundUrl"] absoluteString]];
            }
                
        }
    }];
}


- (void)loginEaseMob
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        HRUpdataUserInfoModel *infoModel = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
        EMError *error = [[EMClient sharedClient] loginWithUsername:infoModel.LocalId password:infoModel.Password];
        if (!error){
            DDLog(@"登录聊天服务器成功");
            [[EMClient sharedClient].options setIsAutoLogin:YES];
            EMPushOptions *options = [[EMClient sharedClient] pushOptions];
            options.displayStyle = EMPushDisplayStyleMessageSummary;
            options.nickname = infoModel.NickName;
            [[EMClient sharedClient] updatePushOptionsToServer];
        }
        
        
    });
}


- (void)postDynamic:(NSString *)soundUrl{
    HRUpdataUserInfoModel*model = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
    
    
    HRPostDynamicRequestModel *requestModel = [HRPostDynamicRequestModel new];
    requestModel.LocalId = USERID;
    requestModel.UserLocalId = USERID;
    requestModel.MomentType = @"1";
    requestModel.MomentDetai = soundUrl;
    requestModel.MomentNotes = [[NSUserDefaults standardUserDefaults] objectForKey:@"fromGuideSoundText"];
    requestModel.PositionName = ADDRESS;
    requestModel.MomentLength = [[NSUserDefaults standardUserDefaults] objectForKey:@"fromGuideSoundLentgh"];
    NSString *Longitude = @"0";
    if ([LONGITUDE length]){
        Longitude = LONGITUDE;
    }
    
    NSString *Latitude = @"0";
    if ([LATITUDE length]){
        Latitude = LATITUDE;
    }
    requestModel.Longitude = Longitude;
    requestModel.Latitude = Latitude;
    requestModel.Permission = 1;
    requestModel.CreateTime = [self getCurrentTime];
    
    [[HRPostDynamicViewModel  new] postDynamicRequstModel:requestModel isSeccess:^(HRDynamicModel *dynamicModel) {
        if (dynamicModel){
//            [self  postSound:@"soundUrl" andImageUrl:[[[NSUserDefaults standardUserDefaults] URLForKey:@"fromGuideImageUrl"] absoluteString]];
            [[NSUserDefaults standardUserDefaults] setURL:nil forKey:@"fromGuideSoundUrl"];
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"fromGuideSoundLentgh"];
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"fromGuideSoundText"];

        }
    }];
    
}

- (void)postSound:(NSString *)soundUrl andImageUrl:(NSString *)imageUrl{
    
    
    
    HRPostSoundRequestModel *requstModel = [HRPostSoundRequestModel new];
    requstModel.LocalId = USERID;
    requstModel.UserLocalId = USERID;
    requstModel.SoundUrl = soundUrl;
    requstModel.SoundLength = [[NSUserDefaults standardUserDefaults] objectForKey:@"fromGuideSoundLentgh"];
    requstModel.CreateTime = [self getCurrentTime];
    requstModel.BackGround = @"soundcover2";
    requstModel.SoundTag = @"3";
    requstModel.BackGroundPhoto = imageUrl;
    
    [[NSUserDefaults standardUserDefaults] setURL:[NSURL URLWithString:soundUrl] forKey:@"SoundUrl"];
    
    WeakSelf
    [self.saveSoundModel postSoundParam:requstModel isSeccess:^(BOOL isSeccess) {
        self.enterButton.enabled = YES;
        
        if (isSeccess){
            [[NSUserDefaults standardUserDefaults] setURL:nil forKey:@"fromGuideSoundUrl"];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}

@end
